//
//  WDInputVinPlateNumberViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/23.
//

#import "WDInputVinPlateNumberViewController.h"
#import "WDInputVinPlateNumberViewModel.h"
#import "WDInputVinPlateNumberView.h"

@interface WDInputVinPlateNumberViewController ()
@property (nonatomic, strong) WDInputVinPlateNumberViewModel *viewModel;

@property (nonatomic, strong) WDInputVinPlateNumberView *vinPlateView;

/// 切换
@property (nonatomic, strong) UIButton *btnChange;

/// 提交
@property (nonatomic, strong) UIButton *btnSubmit;


- (void)initWithBtnChange;

- (void)initWithBtnSubmit;

@end

@implementation WDInputVinPlateNumberViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDInputVinPlateNumberViewModel new];
    _viewModel.inputType = self.inputType;
    [_viewModel setupData];
    debugLog(@"inputType=%zd", self.viewModel.inputType);
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initWithVinPlateView];
    
    [self initWithBtnChange];

    [self initWithBtnSubmit];
}

- (void)initWithVinPlateView {
    if (!_vinPlateView) {
        CGRect frame = CGRectMake(0, 60, kScreenW, kWDInputVinPlateNumberViewHeight);
        _vinPlateView = [[WDInputVinPlateNumberView alloc] initWithFrame:frame];
        [self.view addSubview:_vinPlateView];
    }
    [self.vinPlateView updateWithViewData:self.viewModel];
}

- (void)initWithBtnChange {
    if (!_btnChange) {
        CGRect frame = CGRectMake(20, self.vinPlateView.bottom + 70, kScreenW - 40, 50);
        _btnChange = [UIButton createWithButton:self.view frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithChange:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"切换输入底盘号" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#19C46E"];
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
        }];
        // 输入类型。1 vin后八位；2 车牌号
        if (self.viewModel.inputType == 1) {
            [self.btnChange setTitle:@"切换输入车牌号" forState:UIControlStateNormal];
        } else if (self.viewModel.inputType == 2) {
            [self.btnChange setTitle:@"切换输入底盘号" forState:UIControlStateNormal];
        }
    }
}

- (void)initWithBtnSubmit {
    if (!_btnSubmit) {
        CGRect frame = self.btnChange.frame;
        frame.origin.y = self.btnChange.bottom + 40;
        _btnSubmit = [UIButton createWithButton:self.view frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithSubmit:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"提交" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#19C46E"];
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)actionWithChange:(id)sender {
    // 输入类型。1 vin后八位；2 车牌号
//    debugLog(@"inputtpe=%zd", self.viewModel.inputType);
    if (self.viewModel.inputType == 1) {
        self.viewModel.inputType = 2;
        [self.btnChange setTitle:@"切换输入车牌号" forState:UIControlStateNormal];
    } else if (self.viewModel.inputType == 2) {
        self.viewModel.inputType = 1;
        [self.btnChange setTitle:@"切换输入底盘号" forState:UIControlStateNormal];
    }
    [self.vinPlateView updateWithViewData:self.viewModel];
}

- (void)actionWithSubmit:(id)sender {
    NSString *str = [self.vinPlateView getPlateNo];
    // 1 vin后八位；2 车牌号
    NSDictionary *dict = @{@"code": str, @"inputType":@(self.viewModel.inputType)};
    if (self.popResultBlock) {
        self.popResultBlock(dict);
    }
    [self clickedBack:nil];
    
//    debugLog(@"plate=%@", [self.vinPlateView getPlateNo]);
}

@end
